#include <mysql/mysql.h> 
#include <func.h>//~ - 什么都没有
enum {ID, NAME, USER, PRE_ID, TYPE, MD5, TOMB, PATH, SIGN};
int cd_server(char* path, char* username){
    MYSQL* mysql = mysql_init(NULL);
    MYSQL* consql = mysql_real_connect(mysql, "localhost", "root", "123", "user", 0, NULL, 0);
    if(consql == NULL){
        fprintf(stderr, "Error: %s\n", mysql_error(mysql));
        return -1;
    }
    char buf[1024] = {0};
    sprintf(buf, "select * from fileInfo;");
    int ret = mysql_query(mysql, buf);
    if(ret != 0){
        fprintf(stderr, "Sql Error: %s\n", mysql_error(mysql));
    }
    MYSQL_RES *result = mysql_store_result(mysql);
    printf("result %p\n", result);
    MYSQL_ROW row;
    if(strlen(path) == 0 || *path == '~'){
        char curr[1024] = "UPDATE fileInfo SET sign = 'currpath' WHERE pre_id = -1";
        int ret = mysql_query(mysql, curr);
        if(ret != 0){
            return -1;
        }
        char last[1024] = "UPDATE fileInfo SET sign = 'lastpath' WHERE sign = 'currpath'";
        ret = mysql_query(mysql, last);
        if(ret != 0){
            return -1;
        }
        return 0;
    }
    if(*path == '-'){
        char curr[1024] = "UPDATE fileInfo SET sign = 'currpath' WHERE sign = 'lastpath'";
        int ret = mysql_query(mysql, curr);
        if(ret != 0){
            return -1;
        }
        char last[1024] = "UPDATE fileInfo SET sign = 'lastpath' WHERE sign = 'currpath' and path = ";
        char d[1024] = "'";
        strcat(d, path);
        strcat(d, "'");
        strcat(last, d);
        ret = mysql_query(mysql, last);
        if(ret != 0){
            return -1;
        }
        return 0;
        
    }
//    char temp[1024] = "'";
//    strcat(temp, path);
//    strcat(temp, "'");
//    char* p = strtok(path, "/");
//    while(p){
//        printf("%s\n", p);
//        while((row = mysql_fetch_row(result)) != NULL){
//            if((strcmp(p, row[NAME]) == 0) && (*row[TOMB] == 0) && (*row[TYPE] == 'd') && (strcmp(username, row[USER]))){
//                if(strcmp(temp, row[PATH]) == 0){
//                    char last[1024] = "UPDATE fileInfo SET sign = 'lastpath' WHERE sign = 'currpath'";
//                    int ret = mysql_query(mysql, last);
//                    if(ret != 0){
//                        return -1;
//                    }
//                    char curr[1024] = "UPDATE fileInfo SET sign = 'currpath' WHERE path =  ";
//                    strcat(curr, temp);
//                    ret = mysql_query(mysql, curr);
//                    if(ret != -1){
//                        return -1;
//                    }
//                    return 0;
//                }
//            }
//        }
//        p = strtok(NULL, "/");
//    }
    while((row = mysql_fetch_row(result)) != NULL){
        if((strcmp(path, row[PATH])) && (*row[TOMB] == '0') && (*row[TYPE] == 'd') && (strcmp(username, row[USER]) == 0)){
            char last[1024] = "UPDATE fileInfo SET sign = 'lastpath' WHERE sign = 'currpath'";
            int ret = mysql_query(mysql, last);
            printf("ret = %d\n", ret);
            if(ret != 0){
                return -1;
            }
            char curr[1024] = "UPDATE fileInfo SET sign = 'currpath' WHERE path =  ";
            char d[1024] = "'";
            strcat(d, path);
            strcat(d, "'");
            strcat(curr, d);
            ret = mysql_query(mysql, curr);
            printf("ret = %d\n", ret);
            if(ret != 0){
                return -1;
            }
            return 0;
        }
    }
    mysql_close(consql);
    return -1;
}


